package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.dtr.zxing.activity.MyCaptureActivity;
import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.core.FyBat;
import com.eboss.sbs.enums.EnumConsts;
import com.eboss.sbs.model.fuyou.FyMicropayResponse;
import com.eboss.sbs.model.fuyou.FyQueryResponse;
import com.eboss.sbs.model.fuyou.FyRefundResponse;

public class FYPayActivity extends BaseActivity {

    private Button btnWxPay;
    //    private Button btnWxQry;
    private Button btnWxRfd;
    private Button btnAlyPay;
    //    private Button btnAlyQry;
    private Button btnAlyRfd;
    
    private static final int REQUEST_CAPTURE_WX = 0;
    private static final int REQUEST_CAPTURE_ALY = 1;

    private int qryCount; // 查询标识次数
    private FyBat fybat;
    private String pay_type;

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	mActivity = this;
    	setContentView(R.layout.activity_fypay);
        super.onCreate(savedInstanceState);


//        btnWxQry = (Button) findViewById(R.id.id_wx_qry);
//        btnWxQry.setOnClickListener(this);
//        btnAlyQry = (Button) findViewById(R.id.id_aly_qry);
//        btnAlyQry.setOnClickListener(this);

        findViewById(R.id.id_wx_pay).setOnClickListener(this);
        findViewById(R.id.id_wx_rfd).setOnClickListener(this);
        findViewById(R.id.id_aly_pay).setOnClickListener(this);
        findViewById(R.id.id_aly_rfd).setOnClickListener(this);

        fybat = new FyBat(this, mApp.getLoginEntity().getFyMerchantNo(), new PayResultEvent());

    }

    class PayResultEvent implements FyBat.FYPayResultEvent {

		@Override
		public void onSuccess(FyMicropayResponse data, String client_sn) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSuccess(FyQueryResponse data, String client_sn) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onFailure(int statusCode, String error_msg, String client_sn) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSuccess(FyRefundResponse data, String client_sn) {
			// TODO Auto-generated method stub
			
		}
    	
    }
    
    public void startResultAction(Class<?> cls, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.id_wx_pay:
                startResultAction(MyCaptureActivity.class, REQUEST_CAPTURE_WX);
                break;
            case R.id.id_wx_rfd:
                testRfd(EnumConsts.FYScanChannel.PAY_FY_WX.getChannel());
                break;
            case R.id.id_aly_pay:
                startResultAction(MyCaptureActivity.class, REQUEST_CAPTURE_ALY);
                break;
            case R.id.id_aly_rfd:
                testRfd(EnumConsts.FYScanChannel.PAY_FY_ALY.getChannel());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CAPTURE_WX:
                String result_wx = data.getStringExtra("result");
                //LogUtils.e("result", result_wx);
                testPay(result_wx, EnumConsts.FYScanChannel.PAY_FY_WX.getChannel(), 1);
                break;
            case REQUEST_CAPTURE_ALY:
                String result_aly = data.getStringExtra("result");
                //LogUtils.e("result", result_aly);
                testPay(result_aly, EnumConsts.FYScanChannel.PAY_FY_ALY.getChannel(), 1);
                break;
            default:
                break;
        }
    }

    private void testPay(String qrcode, String type, int amount) {
    	//第一个参数是本地生成订单号 这里无意义
        fybat.pay("111", qrcode, type, amount);
    }

    private void testRfd(String type){
        fybat.refund("111", type, "", 1);
    }

}
